export function percentToPint (percent) {
  return percent.replace('%', '') / 100;
}

export function pintToPercent (pint) {
  return pint * 100 + '%';
}

/**
 * 四舍五入
 * @param s
 * @returns {string}
 */
export function toFixed (s) {
  let originValue = this
  let value = Math.abs(this)
  let changenum = (parseInt(value * Math.pow(10, s) + 0.5) / Math.pow(10, s)).toString();
  let index = changenum.indexOf(".");
  if (index < 0 && s > 0) {
    changenum = changenum + ".";
    for (let i = 0; i < s; i++) {
      changenum = changenum + "0";
    }
  } else {
    index = changenum.length - index;
    for (let i = 0; i < (s - index) + 1; i++) {
      changenum = changenum + "0";
    }
  }
  return originValue > 0 ? changenum : '-' + changenum;
}

/**
 * 不四舍五入保留位数
 * @param stringNumber 字符串数字
 * @param bit 保留位数
 * @returns {*}
 */
export function toFixedNoRound (stringNumber, bit) {
  var _number = '';
  // 补充0
  var _addZero = function (stringNumber, length) {
    for (var i = 0; i < length; i++) {
      stringNumber += '0';
    }
    return stringNumber;
  };
  if (stringNumber !== 0 && !stringNumber) {
    return null;
  } else if (!isNaN(stringNumber)) {
    stringNumber = stringNumber + '';
    var isRadixPoint = stringNumber.indexOf('.') > -1;
    if (isRadixPoint) {//有小数点
      var arr = stringNumber.split('.');
      var integerPart = arr[0];
      var radixPart = arr[1];
      var radixPartLength = radixPart.length;
      if (radixPartLength > bit) {//数字本身的小数位数超过保留位
        radixPart = radixPart.substring(0, bit);
      } else {//数字本身的小数位数超过保留位
        radixPart = _addZero(radixPart, bit - radixPartLength);
      }
      _number = integerPart + '.' + radixPart;
    } else {//没有小数点
      _number = stringNumber + '.' + _addZero('', bit);
    }
  }
  return _number;
}

/**
 * 千分符
 * @param num
 * @returns {string}
 */
export function thousandSeparator (num) {
  if ((!num && num !== 0) || num === '--') return '--'
  let val = num + ''
  let separator = ''
  if (val.indexOf('%') > -1) {
    val = val.split('%')[0]
    separator = '%'
  }
  if (typeof (+val) === 'number') {
    return (val).replace(/(\d)(?=(\d{3})+\.)/g, '$1,') + separator
  } else {
    return '--'
  }
}

/**
 * 生成n~m的随机数
 * @param minNum
 * @param maxNum
 * @returns {number}
 */
export function randomNum(minNum, maxNum) {
  switch (arguments.length) {
    case 1:
      return parseInt(Math.random() * minNum + 1, 10);
    case 2:
      return parseInt(Math.random() * (maxNum - minNum + 1) + minNum, 10);
    default:
      return 0;
  }
}

/**
 * 判断一个数是否为数字
 * @param number
 * @returns {boolean}
 */
export function isNumber (number) {
  return /^-?\d+(\.\d+)?$/.test(number)
}